/*
* pps_bom
*/
package com.pitop.mould.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.mould.entity.Mould;
import com.pitop.mould.service.MouldSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 模具 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/mould")
@Slf4j
@Api(value = "模具控制器", description = "模具控制器")
public class MouldCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private MouldSVImpl mouldSVImpl;


    /**
     * 查询Mould一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Mould mould = mouldSVImpl.load(id, code);
        log.info(JSON.toJSONString(mould));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 查询Mould一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Mould mould = mouldSVImpl.get(id, code);
        log.info(JSON.toJSONString(mould));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }


    /**
     * 根据条件id查询Mould一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Mould mould = mouldSVImpl.loadById(id);
        log.info(JSON.toJSONString(mould));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 根据条件code查询Mould一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Mould mould = mouldSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(mould));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 根据条件id查询Mould一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Mould mould = mouldSVImpl.getById(id);
        log.info(JSON.toJSONString(mould));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 根据条件code查询Mould一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Mould一个详情信息", notes = "查询Mould一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BeanRet getByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Mould mould = mouldSVImpl.getByCode(code);
        log.info(JSON.toJSONString(mould));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 查询Mould信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Mould信息集合", notes = "查询Mould信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Mould mould, @ApiIgnore Page<Mould> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Mould> moulds = mouldSVImpl.list(mould, page.getCurPage(), page.getPageSize());
        int total = mouldSVImpl.count(mould);
        page.setTotalRow(total);
        page.setVoList(moulds);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询Mould信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Mould信息集合", notes = "查询Mould信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<Mould> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Mould> moulds = mouldSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = mouldSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(moulds);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建Mould
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Mould", notes = "创建Mould")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Mould mould) {
        if (StringUtils.isEmpty(mould.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        mouldSVImpl.save(mould);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 修改Mould
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Mould", notes = "修改Mould")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：[开发Develop，稳定Stable，停用 Stop]", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Mould mould) {
        if (StringUtils.isEmpty(mould.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(mould.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(mould.getState())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        mouldSVImpl.modify(mould);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mould);
    }

    /**
     * 根据主键删除Mould
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Mould", notes = "删除Mould")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        mouldSVImpl.delete(id, code);
        return BeanRet.create(true, "删除Mould成功");
    }

}
